package com.google.android.photostream;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/*package*/ class ExecutorFactory {

	private static final int FAST_CORE_POOL_SIZE = 1;
	private static final int FAST_MAXIMUM_POOL_SIZE = 1;
	private static final int FAST_KEEP_ALIVE = 1;

	private static final BlockingQueue<Runnable> sFastWorkQueue = new LinkedBlockingQueue<Runnable>();

	private static final ThreadFactory sFastThreadFactory = new ThreadFactory() {
		private final AtomicInteger mFastCount = new AtomicInteger(1);

		public Thread newThread(Runnable r) {
			return new Thread(r, "ImageTaskF #" + mFastCount.getAndIncrement());
		}
	};

	private static final ThreadPoolExecutor sFastExecutor = new ThreadPoolExecutor(
		FAST_CORE_POOL_SIZE, FAST_MAXIMUM_POOL_SIZE, FAST_KEEP_ALIVE, TimeUnit.SECONDS,
		sFastWorkQueue, sFastThreadFactory);

	public static ThreadPoolExecutor getFastExecutor() {
		return sFastExecutor;
	}

	private static final int SLOW_CORE_POOL_SIZE = 2;
	private static final int SLOW_MAXIMUM_POOL_SIZE = 10;
	private static final int SLOW_KEEP_ALIVE = 10;

	private static final BlockingQueue<Runnable> sSlowWorkQueue = new LinkedBlockingQueue<Runnable>();

	private static final ThreadFactory sSlowThreadFactory = new ThreadFactory() {
		private final AtomicInteger mSlowCount = new AtomicInteger(1);

		public Thread newThread(Runnable r) {
			return new Thread(r, "ImageTask #" + mSlowCount.getAndIncrement());
		}
	};

	private static final ThreadPoolExecutor sSlowExecutor = new ThreadPoolExecutor(
		SLOW_CORE_POOL_SIZE, SLOW_MAXIMUM_POOL_SIZE, SLOW_KEEP_ALIVE, TimeUnit.SECONDS,
		sSlowWorkQueue, sSlowThreadFactory);

	public static ThreadPoolExecutor getSlowExecutor() {
		return sSlowExecutor;
	}
}
